﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using FlexiCommerce.Localization;
using System.Web.DomainServices;
using System.ComponentModel;
using FlexiCommerce.Configuration;

namespace FlexiCommerce.Components
{
    [Serializable]
    public partial class Category : HierarchicalPersistenceItem<Category>, IHierarchicalPersistenceItem
    {
        private string name;
        private string description;
        private IList<CategoryDetails> details = new List<CategoryDetails>();

        [StringLength(20),LocalizedField]
        public String Name
        {
            get
            {
                if (Context.Current.Resolve<EngineSection>().EnableGlobalization)
                {
                    CategoryDetails detail = (from d in details where d.Culture.CultureInfo.Name == System.Threading.Thread.CurrentThread.CurrentUICulture.Name select d).SingleOrDefault();
                    if (detail != null)
                    {
                        return detail.Name;
                    }
                } 
                return name;
            }
            set 
            {
                name = value;
            }
        }

        [StringLength(100), LocalizedField]
        public String Description
        {
            get
            {
                if (Context.Current.Resolve<EngineSection>().EnableGlobalization)
                {
                    CategoryDetails detail = (from d in details where d.Culture.CultureInfo.Name == System.Threading.Thread.CurrentThread.CurrentUICulture.Name select d).SingleOrDefault();
                    if (detail != null)
                    {
                        return detail.Description;
                    }
                }
                return description;
            }
            set
            {
                description = value;
            }
        }

        public IList<CategoryDetails> Details
        {
            get { return details; }
            set { details = value; }
        }

        public void AddDetail(CategoryDetails details)
        {
            details.Category = this;
            Details.Add(details);
        }
        [Exclude]
        public string Url
        {
            get { return Context.UrlParser.BuildUrl(this) ; }
        }
        [NonSerialized]
        private IList<Product> products = new List<Product>();
        public virtual IList<Product> Products
        {
            get { return products; }
            set { products = value; }
        }
    }
}
